var defaultTags = [ "值得信赖", "富有合作精神", "行业大牛", "精通业务", "靠谱的合作伙伴", "能力靠谱", "专业可靠" ], MANX_LEN = 10;

Component({
    properties: {
        data: {
            type: Array,
            value: [],
            observer: function(e) {
                this.count += 1;
                var t = this.data.mode, n = {
                    tags: e
                };
                [ "add", "none" ].includes(t) && (2 === this.count && (t = e.length ? "add" : "none"), 
                "none" === (n.mode = t) && (n.tags = defaultTags.map(function(e, t) {
                    return {
                        name: e,
                        id: Date.now() + t
                    };
                }))), (e || []).some(function(e) {
                    return e.id;
                }) && (n.skeleton = !1), this.setData(n);
            }
        },
        mode: {
            type: String,
            value: "index"
        },
        load: Boolean,
        skeleton: {
            type: Boolean,
            value: !1
        },
        add: {
            type: Boolean,
            value: !1
        }
    },
    data: {
        tags: [],
        defaultTags: []
    },
    created: function() {
        this.count = 0;
    },
    ready: function() {
        this.dialog = this.selectComponent("#dialog"), this.data.skeleton && this.selectComponent("#skeleton").create(this);
    },
    methods: {
        onadd: function() {
            var e = this.data, t = e.tags, n = e.mode, a = t.length;
            if ("none" === n) {
                if (this.isOverLen(t)) return;
                if (a >= MANX_LEN + defaultTags.length) return this.overModal();
            } else if ("add" === n && MANX_LEN <= a) return this.overModal();
            this.dialog.show();
        },
        onConfirm: function(e) {
            var t = e.detail.value, n = this.data, a = n.mode, i = n.tags;
            if (this.isSameName(i, t)) return this.sameNameTip();
            if ("none" === a) {
                var s = {
                    name: t,
                    selected: !0,
                    id: Date.now()
                };
                i.unshift(s), this.setData({
                    tags: i
                }), this.triggerEvent("select", Object.assign({}, s), {});
            } else this.triggerEvent("add", {
                name: t
            });
            this.dialog.hide();
        },
        isOverLen: function(e) {
            var t = e.filter(function(e) {
                return e.selected;
            }).length;
            return MANX_LEN <= t && (this.overModal(), !0);
        },
        overModal: function() {
            wx.showModal({
                title: "最多可添加10个自定义标签",
                content: "精准的标签能更好地帮助访客快速了解你哦！",
                showCancel: !1
            });
        },
        isSameName: function(e, t) {
            return e.some(function(e) {
                return e.name === t;
            });
        },
        sameNameTip: function() {
            wx.showToast({
                image: "/images/warning-circle.png",
                title: "已有同名标签"
            });
        },
        onLike: function(e) {
            var t = e.currentTarget.dataset.index;
            this.triggerEvent("like", {
                index: t
            }, {});
        },
        onSelect: function(e) {
            var t = e.currentTarget.dataset.index, n = this.data, a = n.tags, i = n.mode, s = a[t];
            "none" === i && !s.selected && this.isOverLen(a) || (s.selected = !s.selected, this.setData({
                tags: a
            }), this.triggerEvent("select", Object.assign({}, s), {}));
        }
    }
});